<?php

namespace app\Api\controller;

use app\common\controller\Api;
use app\common\model\LiquorOrder as LiquorOrderModel;

class LiquorOrder extends Api
{
    protected $LiquorOrderModel = null;
    
    public function initialize() {
        $this->LiquorOrderModel = new LiquorOrderModel();
    }
    
    public function index()
    {
        $begintime=strtotime(date("Y-m-d",strtotime("-3 day"))." 0:0:0");
        $condition = [
            ['ctime', ['EGT',$begintime],['ELT',time()],'and'],
            ['user_id', 'eq', $this->userId]
        ];
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', 20);
        $liquor_model = $this->LiquorOrderModel;
        $this->result['data'] = $liquor_model::with(['liquor' => ['goods']])->where($condition)->order('id', 'desc')->limit(($page-1)*$size, $size)->select();
        $this->result['data'] = obj_to_array($this->result['data']);
        foreach ($this->result['data'] as &$info) {
            $info['ctime'] = date('Y-m-d H:i:s', $info['ctime']);
        }
        return $this->result;
    }
    
    public function cancel()
    {
        return $this->LiquorOrderModel->cancel($this->request->post('id'), $this->userId);
    }
}
